﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpenseSystem.Fether;
using ExpenseSystem.Common.BaseModel;
using ExpenseSystem.Common;

namespace ExpenseSystem.Core
{
    public class ApprovedHelper<T>where T : NormalModel, new()
    {
        protected UserProfile profile = Authority.Authority.GetUserProfile();
        public ApprovedHelper(NormalFetcher<T> fetcher,INormalManager<T> manager)
        {
            this.manager = manager;
                 
            this.fetcher = fetcher;
        }
        INormalManager<T> manager;
        NormalFetcher<T> fetcher;
        public bool Submit(long modelid, out string message)
        {
            T model = fetcher.Get(profile.SiteId, modelid, out message);
            if (model == null)
            {
                message = "无法找到对应的记录";
                return false;
            }
            model.ResetUpdateTrace();
            if (model.State != Definition.STATE_NORMAL)
            {
                message = "只有在“正常”状态的单据才能提交";
                return false;
            }
            model.State = Definition.STATE_READY;
            return manager.Save(model, out message);
        }
        public bool Recall(long modelid, out string message)
        {
            T model = fetcher.Get(profile.SiteId, modelid, out message);
            if (model == null)
            {
                message = "无法找到对应的记录";
                return false;
            }
            model.ResetUpdateTrace();
            if (model.State != Definition.STATE_READY)
            {
                message = "只有在“提交”状态的单据才能撤回";
                return false;
            }
            model.State = Definition.STATE_NORMAL;

            return manager.Save(model, out message);
        }
        public bool Approved(long modelid, out string message)
        {
            T model = fetcher.Get(profile.SiteId, modelid, out message);
            if (model == null)
            {
                message = "无法找到对应的记录";
                return false;
            }
            model.ResetUpdateTrace();
            if (model.State != Definition.STATE_READY)
            {
                message = "只有在“提交”状态的单据才能审批";
                return false;
            }
            model.State = Definition.STATE_APPROVED;
            model.ApprovedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
            model.Approver = profile.UserCode;
            return manager.Save(model, out message);
        }
        public bool Reject(long modelid, out string message)
        {
            T model = fetcher.Get(profile.SiteId, modelid, out message);
            if (model == null)
            {
                message = "无法找到对应的记录";
                return false;
            }
            model.ResetUpdateTrace();
            if (model.State != Definition.STATE_READY)
            {
                message = "只有在“提交”状态的单据才能退回";
                return false;
            }
            model.State = Definition.STATE_NORMAL;
            model.ApprovedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
            model.Approver = profile.UserCode;
            return manager.Save(model, out message);
        }
        public bool Comfirm(long modelid, out string message)
        {
            T model = fetcher.Get(profile.SiteId, modelid, out message);
            if (model == null)
            {
                message = "无法找到对应的记录";
                return false;
            }
            model.ResetUpdateTrace();
            if (model.State != Definition.STATE_APPROVED)
            {
                message = "只有在“审核”状态的单据才能确认";
                return false;
            }
            model.State = Definition.STATE_CONFIRM;
            model.ConfirmedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
            model.Confirmed = profile.UserCode;
            return manager.Save(model, out message);
        }
        public bool Revert(long modelid, out string message)
        {
            T model = fetcher.Get(profile.SiteId, modelid, out message);
            if (model == null)
            {
                message = "无法找到对应的记录";
                return false;
            }
            model.ResetUpdateTrace();
            if (model.State != Definition.STATE_APPROVED)
            {
                message = "只有在“审核”状态的单据才能反审";
                return false;
            }
            model.State = Definition.STATE_NORMAL;
            model.ApprovedDate = DateTime.Now.ToString(Definition.DATETIME_FORMAT);
            model.Approver = profile.UserCode;
            return manager.Save(model, out message);
        }
    }
}
